<template>
  <el-form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <el-form-item prop="userName">
      <el-input v-model="form.userName" placeholder="请输入用户名">
        <span slot="prepend">
          <el-icon :size="16" type="user" class="el-icon-user-solid" />
        </span>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="form.password" type="password" placeholder="请输入密码">
        <span slot="prepend">
          <el-icon :size="14" type="lock" class="el-icon-lock" />
        </span>
      </el-input>
    </el-form-item>
    <el-form-item>
      <div align="center">
        <el-button size="large" type="primary" class="login-button" @click="handleSubmit">登录</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  data() {
    return {
      form: {
        userName: '',
        password: ''
      }
    }
  },
  computed: {
    rules() {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    handleSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit('on-success-valid', {
            userName: this.form.userName,
            password: this.form.password
          })
        }
      })
    }
  }
}
</script>
